﻿using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using System.Text;
using System.Windows.Threading;

namespace NB_Controller_Test
{
    public class SignalData
    {
        public int nCount = 0;
        public string strIPAndTag = "";
        public ObservableDataSource<Point> mSignalRecord = new ObservableDataSource<Point>();
        public LineGraph lineGp = null;

        public string strMac = "";
        public string strAliasName = "";
        public string strImei = "";

        public DateTime dtStartTime;
        //string strAverageValue = "";

        public bool IsIPAndTagExist(string strIp)
        {
            return strIPAndTag == strIp;
        }

        public void AddNewPoint(Dispatcher dsp, Point pt)
        {
            mSignalRecord.AppendAsync(dsp, pt);
        }
    }

    public class SignalDataManger
    {
        public List<SignalData> SignalDatas = new List<SignalData>();

        public class CComparer : IComparer<SignalData>
        {
            public int Compare(SignalData left, SignalData right)
            {
                if (left.strIPAndTag == right.strIPAndTag)
                    return 0;
                else
                    return -1;
            }
        }

        public bool IsIPExist(string strIp)
        {
            foreach (SignalData sd in SignalDatas)
            {
                if (sd.IsIPAndTagExist(strIp))
                    return true;
            }

            return false;
        }

        public SignalData GetSignalData(string strIp, bool bCreatNewIfNotExist, out bool bNewCreate)
        {
            SignalData data = GetData(strIp);
            if (data == null && bCreatNewIfNotExist == true)
            {
                SignalData sdNew = new SignalData();
                sdNew.strIPAndTag = strIp;

                SignalDatas.Add(sdNew);
                //SignalDatas.Sort(new CComparer());

                bNewCreate = true;
                return sdNew;
            }
            else
            {
                bNewCreate = false;
                return data;
            }
        }

        public SignalData GetData(string strIp)
        {
            foreach (SignalData sd in SignalDatas)
            {
                if (sd.IsIPAndTagExist(strIp))
                    return sd;
            }

            return null;
        }
    }
}
